<?php
class BankPaymentHandler {
	
	/**
	 * Returns 2D array of usable banks.
	 * Inner array keys:
	 * <ul>
	 * <li>key - bank key</li>
	 * <li>icon - url to icon with dimensions 88x31 px</li>
	 * </ul>
	 * 
	 *
	 * @return Array
	 */
	public static function getUsableBanks() {
		$confPath = BankLink::getConfFilePath();
		if( !is_file($confPath) )
			throw new  RuntimeException('Can\'t load BankLink configuration file!');
		
		require( $confPath );
		
		$data = array();
		foreach( $BANKLINK as $key => $bank ) {
			if( $bank['usable'] === true ) {
				$data[] = array(
					'key' => $key,
					'icon' => DOM_URL.'../libsPub/BankLink/images/'.$key.'.gif',
				);
			}
		}
		return $data;
	}
	
	/**
	 * Sends user to bank. echoes some HTML and calls die()
	 * That HTML directs user to bank.
	 */
	public static function sendToBank( BankPayment $payment ) {
	
		$entry = new BankLog();
		$entry->sendIp = ip();
		$entry->sendTime = strtotime('now');
		$entry->uid = (string)$payment->uid;
		$entry->userData = $payment->userData;
		$entry->description = $payment->description;
		$entry->price = $payment->price;
		$entry->currency = $payment->currency;
		$entry->status = 0;
		$entry->bank = $payment->bank;
	
		if( $entry->save() ) {
			$link = BankLink::getLink( $payment->bank );		
			$link->sendToBank( $entry->id, $payment );
		} else {
			throw new RuntimeException( implode('<br>',DomArValidationReport::getLastReport()->messages ) );		
		}
		
	}
	
	/**
	* Handle bank replay
	* @var String $bank
	* @return BankPaymentReplay
	*/
	public static function handleReplay() {
		
		$replayData =  BankLink::handleReplay();
		$logEntry = BankLog::query()->where('id = '.(int)$replayData['logId'].'')->limit(1)->commit();
		$replay = new BankPaymentReplay();
		
		// payment failed with unknown error or log entry not found
		if( $replayData['status'] == 0 or !$logEntry ) {
			return $replay;
		}
		
		// prepare replay
		$replay->logId = $logEntry->id;
		$replay->uid = $logEntry->uid;
		$replay->userData = $logEntry->userData;
		$replay->description = $logEntry->description;
		$replay->price = $logEntry->price;
		$replay->currency = $logEntry->currency;
		$replay->bank = $logEntry->bank;
		
		
		// handling new payment
		if( $logEntry->status === 0 ) {
			$logEntry->status = (int)$replayData['status'];
			$status = $logEntry->status;
			$logEntry->backIp = ip();
			$logEntry->backTime = new DomArDateTime('now');
			$logEntry->bankError = $replayData['bankError'];
			$logEntry->bankErrorStr = $replayData['bankErrorStr'];
			if( !$logEntry->save() )
				throw new RuntimeException('Saving log entry failed! Reasons: '.implode(',',$logEntry->getValidationReport()->messages) ) ;
		
		// payment already successfully handled
		} else if( $logEntry->status == 1 ){
			$status = 2;
		
		// payment has failed
		} else {
			$status = $logEntry->status;
		}
		
		// finishing replay
		$replay->status = $status;
		$replay->bankError = $logEntry->bankError;
		$replay->bankErrorStr = $logEntry->bankErrorStr;
			
		return $replay;
	}
	
}
?>